//
//  ViewController.m
//  瀑布流
//
//  Created by 梅敏杰 on 2018/6/7.
//  Copyright © 2018年 梅敏杰. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
/** 所有的商品数据 */
@property (nonatomic, strong) NSMutableArray *shops;
@property (weak ,nonatomic)UICollectionView * collectionView;
@end

@implementation ViewController
static NSString * const mmjShopId = @"shop";
-(NSMutableArray *)shops{
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return _shops;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];

    [self setupLayout];
    
    [self setupRefresh];

//
}
-(void)setupLayout{
        //创建布局
        mmjWaterFlow * layout = [[mmjWaterFlow alloc]init];
    layout.delegate = self;
        //创建collectionview
    //    CGFloat collectionW = self.view.frame.size.width;
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        collectionView.dataSource = self;
    //    collectionView.delegate = self;
        [self.view addSubview:collectionView];

        //注册
//        [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:mmjShopId];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([mmjShopCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:mmjShopId];
    
    self.collectionView = collectionView;
}
-(void)setupRefresh{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewShops)];
    [self.collectionView.mj_header beginRefreshing];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
    self.collectionView.mj_footer.hidden = YES;
}
-(void)loadNewShops{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [mmjShopModel mj_objectArrayWithFilename:@"1.plist"];
        [self.shops removeAllObjects];
        [self.shops addObjectsFromArray:shops];
        
        // 刷新数据
        [self.collectionView reloadData];
        
        [self.collectionView.mj_header endRefreshing];
    });
}
-(void)loadMoreShops{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [mmjShopModel mj_objectArrayWithFilename:@"1.plist"];
        [self.shops addObjectsFromArray:shops];
        
        // 刷新数据
        [self.collectionView reloadData];
        
        [self.collectionView.mj_footer endRefreshing];
    });
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    self.collectionView.mj_footer.hidden = self.shops.count == 0;
    return self.shops.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    mmjShopCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:mmjShopId forIndexPath:indexPath];
    cell.model = self.shops[indexPath.item];

    return cell;
}
#pragma
-(CGFloat)waterflowLayout:(mmjWaterFlow *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth{
    mmjShopModel * shop = self.shops[index];
    
    return itemWidth * shop.h / shop.w;
}
@end
